﻿Option Explicit On

Imports System.Xml
Imports System.IO

Public Class frmFerramentasANSIToUTF8

    Private Sub tbArquivoOrigem_ItemClicked(sender As System.Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles tbArquivoOrigem.ItemClicked
        Try
            Select Case e.ClickedItem.Tag
                Case "OP"
                    Call AbrirXML()
                Case "CNV"
                    Call ConverterToUTF8()
            End Select
        Catch ex As Exception
            Call SendError(ex.Message, ex.StackTrace)
        End Try
    End Sub

    Private Sub tbArquivoDestino_ItemClicked(sender As System.Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles tbArquivoDestino.ItemClicked
        Try
            Select Case e.ClickedItem.Tag
                Case "GR"
                    Call GravarArquivo()
            End Select
        Catch ex As Exception
            Call SendError(ex.Message, ex.StackTrace)
        End Try
    End Sub

    ''' <summary>
    ''' Abre documento XML e valida-o
    ''' </summary>
    ''' <remarks>3.4.3.26</remarks>
    Private Sub AbrirXML(Optional ByVal strFileName As String = "")
        Try
            Dim strValElement As String = ""
            Dim strXML As String = ""
            Dim MyXml As New XmlDocument()
            Dim myFileName As OpenFileDialog = New OpenFileDialog()
            If IsEmpty(strFileName) Then
                With myFileName
                    .Title = "Arquivos XML"
                    .Filter = "Arquivos XML (*.xml)|*.xml"
                    .ShowDialog()
                    strFileName = .FileName
                End With
            End If
            If Trim(strFileName) = "" Then Exit Sub

            Dim fluxoTexto As IO.StreamReader
            Dim linhaTexto As String, strTexto As String = ""

            If IO.File.Exists(strFileName) Then
                fluxoTexto = New IO.StreamReader(strFileName)
                linhaTexto = fluxoTexto.ReadLine

                While linhaTexto <> Nothing
                    strTexto &= linhaTexto
                    linhaTexto = fluxoTexto.ReadLine
                End While
                fluxoTexto.Close()
            Else
                MessageBox.Show("Arquivo não existe")
                Exit Sub
            End If

            txtXMLOrigem.Text = strTexto

        Catch ex As Exception
            Call SendError(ex.Message, ex.StackTrace)
        End Try
    End Sub


    Private Sub ConverterToUTF8()
        Dim strTexto As String = txtXMLOrigem.Text

        txtXMLDestino.Text = UTF8_Encode(strTexto)

    End Sub

    Private Sub GravarArquivo()
        Dim salvarComo As SaveFileDialog = New SaveFileDialog()
        Dim caminho As DialogResult
        Dim fluxoTexto As IO.StreamWriter
        Dim Arquivo As String

        salvarComo.CheckFileExists = False
        salvarComo.Title = "Arquivo XML |*.xml |"
        caminho = salvarComo.ShowDialog
        Arquivo = salvarComo.FileName

        If Arquivo = Nothing Then
            MessageBox.Show("Arquivo Invalido", "Salvar Como", MessageBoxButtons.OK)
        Else
            fluxoTexto = New IO.StreamWriter(Arquivo, True, System.Text.Encoding.UTF8)
            fluxoTexto.Write(txtXMLDestino.Text)
            fluxoTexto.Close()
        End If
    End Sub


End Class